export const ajaxPost = (url, data) => {
  return new Promise(function (resolve, reject) {
    let xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
    xhr.onload = function () {
      if (xhr.status === 200) {
        const res = JSON.parse(xhr.response);
        if (res.code == 0) {
          resolve(res.data);
        }
        reject(Error(xhr.statusText));
      } else {
        reject(Error(xhr.statusText));
      }
    };
    xhr.onerror = function () {
      reject(Error('Network Error'));
    };
    xhr.send(JSON.stringify(data));
  });
};

export const ajaxUpload = (url, data, type) => {
  return new Promise(function (resolve, reject) {
    let xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    xhr.setRequestHeader('Content-Type', 'multipart/form-data');
    xhr.onLoad = function () {
      if (xhr.status == 200) {
        const res = JSON.parse(xhr.response);
        if (res.code == 0) {
          resolve(res.data);
        }
        reject(Error(xhr.statusText));
      }
    };
    xhr.onerror = function () {
      reject(Error('Network Error'));
    };
    xhr.send(JSON.stringify(data));
  });
};
